﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Expression.Dev.Dao;
using T.FAS.Runtime.Expression.Entity;
using T.FAS.Runtime.Base.Entity;
using System.Collections.Concurrent;

namespace T.FAS.Runtime.Expression.Service
{
    class DebugCache
    {
        private readonly ConcurrentDictionary<string, DebugInfo> debugInfos = new ConcurrentDictionary<string, DebugInfo>();

        private static readonly object syncObject = new object();

        private static DebugCache instance;

        private DebugCache()
        {
            debugInfos = new ConcurrentDictionary<string, DebugInfo>();
        }

        public static DebugCache GetInstance()
        {
            if (instance == null)
            {
                lock (syncObject)
                {
                    if (instance == null)
                        instance = new DebugCache();
                }
            }
            return instance;
        }
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="requestID"></param>
        /// <param name="debugInfo"></param>
        public void SetDebugCache(string requestID, DebugInfo debugInfo)
        {
            debugInfos[requestID] = debugInfo;
        }
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="requestID"></param>
        /// <returns></returns>
        public DebugInfo GetDebugCache(string requestID)
        {
            if (debugInfos.ContainsKey(requestID))
            {
                return debugInfos[requestID];
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="requestID"></param>
        public void RemoveDebugCache(string requestID)
        {
            if (debugInfos.ContainsKey(requestID))
            {
                DebugInfo debugInfo;
                debugInfos.TryRemove(requestID, out debugInfo);
            }
        }
    }
}
